﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplicationGDI6._1Home
{
    public class Enumer
    {        
        Type enumType;

        ComboBox cb;

        public delegate void EnumChanged(Enumer sender, object value);

        public event EnumChanged onChanged;

        public Enumer(Type enumType, Form form)
        {
            this.enumType = enumType;

            cb = new ComboBox();
            cb.MaxDropDownItems = 3;
            cb.Text = "unit";
            cb.Width = 121;
            cb.Height = 21;

            cb.DropDownStyle = ComboBoxStyle.DropDown;
            cb.SelectedValueChanged += new EventHandler(cb_SelectedValueChanged);

            foreach (var item in Enum.GetValues(enumType))
            {
                cb.Items.Add(item);
            }

            form.Controls.Add(cb);
        }

        public void SetComboBoxPosition(int x, int y)
        {
            cb.Left = x;
            cb.Top = y;
            cb.SelectedIndex = 1;
        }

        void cb_SelectedValueChanged(object sender, EventArgs e)
        {
            ComboBox cb = sender as ComboBox;
            //cb.SelectedIndex = 48;
            if (cb.SelectedItem == null)
            {
                return;
            }

            object value = Enum.Parse(enumType, cb.SelectedItem.ToString());

            if (onChanged != null)
            {
                onChanged(this, value);
            }
        }
    }
}